const express = require('express');
const mysqlExec = require('../db/dbMysql.js');
const { Msg, curPath,useJWT } = require('../tools/msg.js');
const {
    v4: uuidv4
  } = require('uuid'); //生成唯一字符串
const router = express.Router();

//搜索接口
router.get('/search',async function(req,res){
    let{keyword = '', limits = 6, pages = 1}=req.query;
    if(!keyword){
        res.send(Msg(500, '搜索关键词不能为空'));
        return;
    }
    //游标位置
    let starts=(pages-1)*limits;
    let sql=`select cid,title,price,concat('${curPath}course/',image_src) as image_src,area_name,subject_name from e_course where title like '%${keyword}%' limit ${starts},${limits}`;
    let [err,arr]=await mysqlExec(sql);
    if(err){
        //总数
        sql=`select count(cid)as n from e_course where title like '%${keyword}%'`;
        let [e,arr2]=await mysqlExec(sql);
        if(e){
            arr.unshift(arr2[0].n);
        }else{
            arr.unshift(0);
        }
        res.send(Msg(200, '课程查询成功', arr))
    }else{
        res.send(Msg(500, '课程查询失败'));
    }
});

//报名学习
router.post('/study',async(req,res)=>{
    let{cid='1beec178-4b02-40fb-9d1c-8aa61cd4a097',jwtdata=''}=req.body;
    if(cid==''){
        res.send(Msg(500,'课程id不能为空'));
        return;
    }
    if(!jwtdata){
        res,send(Msg(500,'jwt数据不能为空'));
    }
    //使用jwt数据
    let obj=useJWT(jwtdata);
    if(!obj){
        res.send(Msg(500,'无效的jwt数据'));
        return;
    }
    //查询当前用户是否报名过当前课程
    let sql=`select count(id) as n from e_order where cid='${cid}' and mid='${obj.uid}'`;
    let [e,arr]=await mysqlExec(sql);
    if(e){
        if(arr[0].n==1){
            res.send(Msg(500,'当前课程已经报名过'));
            return;
        }
    }else{
        res.send(Msg(500,'查询失败'));
        return;
    }
    sql=`insert into e_order(oid,cid,mid,createdate)value('${uuidv4()}','${cid}','${obj.uid}','${new Date().getTime()}')`;
    let [err,data]=await mysqlExec(sql);
    if(err){
        if(data.affectedRows==1){
            res.send(Msg(200,'报名学习成功'));
        }else{
            res.send(Msg(500,'报名学习失败'));
        }
        
    }else{
        res.send(Msg(500,'报名学习失败'));
    }

})

router.get('/details',async(req,res)=>{
    let {cid = '1beec178-4b02-40fb-9d1c-8aa61cd4a097'}=req.query;
    if(!cid){
        res.send(Msg(500,'课程ia不能为空'));
        return;
    }

    //根据课程id查询课程及老师的基本信息
    sql = `select cid,title,price,concat('${curPath}course/',image_src) as image_src,c.intro as cintro,area_name,grade_name,subject_name,realname,t.intro,concat('${curPath}avatar/',head_photo_url) as head_photo_url from e_course as c left join e_teacher as t on c.tid=t.tid where cid='${cid}'`;
    let [err,arr]=await mysqlExec(sql);
    if(err){
        //根据id查询课程信息的视频信息
        sql=`select video_src,video_title from e_video where cid='${cid}'`;
        let [e,datas]=await mysqlExec(sql);
        if(e){
            arr.unshift(datas);
        }else{
            arr.unshift([])
        }
        res.send(Msg(200,'课程详情查询成功',arr));
    }else{
        res.send(Msg(500,'课程详情查询失败'))
    }
})

module.exports=router;